var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
		userinfo: [],
		money: 0,
		sysset: false,
		paytype:'微信钱包',
	},
	_onLoad: function (options) {
		var that = this
		wx.setNavigationBarTitle({
			title: (that.data.textset['佣金'] || '佣金')+'提现'
		});
		that.setData({ show: 1 })
		$.get('agent/commissionWithdraw', {}, function (res) {
				that.setData(res);
				var sysset = res.sysset;
				var paytype = '微信钱包';
				if(sysset.withdraw_weixin==1){
					paytype = '微信钱包'
				}
				if(!sysset.withdraw_weixin || sysset.withdraw_weixin==0){
					paytype = '支付宝'
				}
				if((!sysset.withdraw_weixin || sysset.withdraw_weixin==0)&&(!sysset.withdraw_aliaccount || sysset.withdraw_aliaccount==0)){
					paytype = '银行卡';
				}
				that.setData({paytype:paytype})
		})
	},
	onShow: function (){
		this._onLoad();	
	},
	moneyinput: function (e) {
		var that = this
		var usermoney = parseFloat(this.data.userinfo.commission);
		var money = parseFloat(e.detail.value);
		if (money < 0) {
			$.error('必须大于0');
		} else if (money > usermoney) {
			$.error('可提现'+(that.data.textset['佣金'] || '佣金')+'不足');
			return {
				value: usermoney,
			}
		} else {
			this.setData({ money: money })
		}
	},
	paytypechange:function(e){
		var that = this
		var paytype = e.detail.value
		that.setData({paytype:paytype})
	},
	formSubmit: function (e) {
		var that = this
		var usermoney = parseFloat(this.data.userinfo.commission);
		var withdrawmin = parseFloat(this.data.sysset.withdrawmin);
		//var formdata = e.detail.value;
		var money = parseFloat(this.data.money);
		var paytype = this.data.paytype;
		if (isNaN(money) || money <= 0) {
			$.error('提现金额必须大于0');
			return;
		}
		if (withdrawmin > 0 && money < withdrawmin) {
			$.error('提现金额必须大于¥' + withdrawmin);
			return;
		}
		if (money > usermoney) {
			$.error((that.data.textset['余额'] || '余额')+'不足');
			return;
		}
		if(paytype == '支付宝' && !this.data.userinfo.aliaccount){
			$.alert('请先设置支付宝账号', function () {
				$.goto('../my/set');
			});
			return;
		}
		if(paytype == '银行卡' && (!this.data.userinfo.bankname || !this.data.userinfo.bankcarduser || !this.data.userinfo.bankcardnum)){
			$.alert('请先设置完整银行卡信息', function () {
				$.goto('../my/set');
			});
			return;
		}
		$.post('agent/commissionwithdraw', { money: money, paytype: paytype}, function (data) {
			if (data.status == 0) {
				$.error(data.msg);
				return;
			} else {
				$.success(data.msg);
				that.subscribeMessage(function(){
					setTimeout(function () {
						$.goto('commissionlog?st=1');
					}, 1000)
				})
			}
		}, '提交中')
	}
}))